<?php

namespace App\Core;

use App\Constants\AppInfo;
use Monolog\DateTimeImmutable;
use Monolog\Logger;

class MallLogger extends Logger
{
    private $traceNo = '';

    /**
     * 执行的action
     * @var string
     */
    private $handlerAction = '';


    /**
     * @param mixed|string $traceNo
     */
    public function setTraceNo($traceNo): void
    {
        $this->traceNo = $traceNo;
    }

    /**
     * @return string
     */
    public function getTraceNo(): string
    {
        return $this->traceNo;
    }

    /**
     * @return string
     */
    public function getHandlerAction(): string
    {
        return $this->handlerAction;
    }



    /**
     * @param string $handlerAction
     */
    public function setHandlerAction(string $handlerAction): void
    {
        $this->handlerAction = $handlerAction;
    }


    private function formateMessage($message)
    {
        return $this->handlerAction . ' || ' . $this->traceNo . ' || ' . $message;
    }


    /**
     * @inheritDoc
     */
    public function addRecord(int $level, string $message, array $context = [], DateTimeImmutable $datetime = null): bool
    {
        $message = $this->formateMessage($message);
        return parent::addRecord($level, $message, $context);
    }

    /**
     * Adds a log record at the ERROR level.
     *
     * This method allows for compatibility with common interfaces.
     *
     * @param string $message The log message
     * @param array $context The log context
     */
    public function error($message, array $context = []): void
    {
        parent::error($message, $context);
        try {
            $appErrorService = new AppErrorService();
            $appErrorService->setRedis(AppInfo::$container->get(\RedisCluster::class));
            $appErrorService->setLogger(AppInfo::$container->get(Logger::class));
            $appErrorService->errorLogNotice($message,$context);
        } catch (\Throwable $e) {
            if(HLL_IS_DEV){
                throw $e;
            }
        }

    }


}